package com.java.java基础.线程;

public class ThreadMethodExcercisc {
    public static void main(String[] args) throws InterruptedException {
        Thread t = new Thread(new T1());
        for (int i = 1; i <= 10; i++){
            System.out.println("hi="+i);
            if (i == 5) { // 说明主线程输出了5次hi
                System.out.println("hello");
                t.start();  // 启动了子线程 输出hello
                t.join(); // 立即将主线程挂起，子线程执行，等到子线程执行完再继续执行主线程
            }
            Thread.sleep(1000); // 输出一个hi 休眠一秒
        }
        System.out.println("main线程退出");
        System.out.println("这是在干嘛呢");
    }
}

class T1 implements Runnable{
    private int count;
    @Override
    public void run()
    {
        while (true){
            System.out.println("hello="+count++);
            try{
                // 休眠1秒
                Thread.sleep(1000);
            }catch (InterruptedException e) {
                e.printStackTrace();
            }
            // 直到count=10 退出循环
            if (count == 10){
                System.out.println("hello线程退出");
                break;
            }
        }
    }
}
